package com.dlna.client.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.dlna.client.R;
import com.dlna.client.enity.FileInfo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class VideoAdapter extends BaseAdapter {
	private LayoutInflater mInflater;

	private List<FileInfo> list;

	private DisplayImageOptions options;

	public VideoAdapter(Context c, List<FileInfo> list) {
		mInflater = LayoutInflater.from(c);
		this.list = list;
		options = new DisplayImageOptions.Builder()
				.bitmapConfig(Bitmap.Config.RGB_565)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.build();
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {

		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.videoimage_item, null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView.findViewById(R.id.video);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.imageView.setImageResource(R.drawable.video);
		ImageLoader
				.getInstance()
				.displayImage(
						list.get(position).filePath,
						holder.imageView,
						options,
						new SimpleImageLoadingListener() {
							
							String message = "";
							@Override
							public void onLoadingStarted(String imageUri,
									View view) {
							}

							@Override
							public void onLoadingFailed(String imageUri,
									View view, FailReason failReason) {
								switch (failReason.getType()) {
								case IO_ERROR:
									message = "Input/Output error";
									break;
								case DECODING_ERROR:
									message = "Image can't be decoded";
									break;
								case NETWORK_DENIED:
									message = "Downloads are denied";
									break;
								case OUT_OF_MEMORY:
									message = "Out Of Memory error";
									break;
								case UNKNOWN:
									message = "Unknown error";
									break;
								}
							}

							@Override
							public void onLoadingComplete(String imageUri,
									View view, Bitmap loadedImage) {

							}
						},
						com.nostra13.universalimageloader.core.ImageLoader.MediaType.VIDEO);

		convertView
				.setBackgroundResource(R.drawable.grid_view_item_selector_xml);
		// convertView.setBackgroundResource(R.drawable.login_btn_selector);
		return convertView;
	}

	public class ViewHolder {
		ImageView imageView;
	}
}
